<html>
<head><meta charset="utf-8"><title>Alternative to `as` for truncation? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html">Alternative to `as` for truncation?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217480650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480650">(Nov 21 2020 at 02:36)</a>:</h4>
<p>If I want to get the low 8 bits of a <code>u64</code> as a <code>u8</code> (where truncation is intended to happen), is there an alternative to <code>x as u8</code> to represent this more explicitly? <code>Wrapping</code> doesn't seem to have anything useful and there is a (IMO reasonable) clippy lint against <code>as</code>.</p>



<a name="217480706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480706">(Nov 21 2020 at 02:38)</a>:</h4>
<p>You could use the byte array conversion and then get the right byte</p>



<a name="217480752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480752">(Nov 21 2020 at 02:38)</a>:</h4>
<p>that's a bit weird since I want to get <code>x % 256</code> and don't care about byte order</p>



<a name="217480820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480820">(Nov 21 2020 at 02:40)</a>:</h4>
<p>Ooh, what if <code>Wrapping&lt;u64&gt;: Into&lt;Wrapping&lt;u8&gt;&gt;</code></p>



<a name="217480825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480825">(Nov 21 2020 at 02:40)</a>:</h4>
<p>Hm why not do % 256 then?</p>



<a name="217480830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480830">(Nov 21 2020 at 02:41)</a>:</h4>
<p>because it is still a u64</p>



<a name="217480831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480831">(Nov 21 2020 at 02:41)</a>:</h4>
<p>I don't think we should implement into</p>



<a name="217480835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480835">(Nov 21 2020 at 02:41)</a>:</h4>
<p>I think the into impl for the respective <code>Wrapping</code> structs makes sense from a modular arithmetic POV</p>



<a name="217480838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480838">(Nov 21 2020 at 02:41)</a>:</h4>
<p>I'm generally against implementing traits for things that don't want to be used generically</p>



<a name="217480880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480880">(Nov 21 2020 at 02:42)</a>:</h4>
<p>?</p>



<a name="217480885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480885">(Nov 21 2020 at 02:42)</a>:</h4>
<p>Aren't most <code>From</code> impls in that category?</p>



<a name="217480894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480894">(Nov 21 2020 at 02:43)</a>:</h4>
<p>You almost certainly don't want a function taking <code>impl Into&lt;Wrapping&lt;u8&gt;&gt;</code>, and it's not an obvious conversion from u64 IMO</p>



<a name="217480935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480935">(Nov 21 2020 at 02:44)</a>:</h4>
<p>I don't think they are, no</p>



<a name="217480939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480939">(Nov 21 2020 at 02:44)</a>:</h4>
<p>It's the quotient map <code>Z/2^64 -&gt; Z/2^8</code></p>



<a name="217480942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480942">(Nov 21 2020 at 02:44)</a>:</h4>
<p>I don't disagree</p>



<a name="217480958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217480958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217480958">(Nov 21 2020 at 02:45)</a>:</h4>
<p>in fact it <em>only</em> makes sense going "down" the types, rather than up as with the regular numeric types</p>



<a name="217481009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481009">(Nov 21 2020 at 02:46)</a>:</h4>
<p>But I would be opposed, because though there's not obvious other behavior, I would not want silent truncation like that</p>



<a name="217481014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481014">(Nov 21 2020 at 02:46)</a>:</h4>
<p>I think a truncate method would be great, though</p>



<a name="217481023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481023">(Nov 21 2020 at 02:47)</a>:</h4>
<p>it's not silent, there's a conversion</p>



<a name="217481069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481069">(Nov 21 2020 at 02:48)</a>:</h4>
<p>I don't like lossy conversions in From/Into, and consider them an antipattern.</p>



<a name="217481082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481082">(Nov 21 2020 at 02:49)</a>:</h4>
<p>Hm, I'm not even sure I want to call it lossy, it's a faithful representation of the value in the target type</p>



<a name="217481092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481092">(Nov 21 2020 at 02:49)</a>:</h4>
<p>A <code>truncate</code> method would be more terse in my setting though, because I'm not using <code>Wrapping</code> currently</p>



<a name="217481145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481145">(Nov 21 2020 at 02:50)</a>:</h4>
<p>It's definitely what I would call lossy. I don't think "faithful representation" is wrong, just sort of unhelpful</p>



<a name="217481154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481154">(Nov 21 2020 at 02:51)</a>:</h4>
<p>Same as how I wouldn't want impls from various types to bool</p>



<a name="217481172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481172">(Nov 21 2020 at 02:52)</a>:</h4>
<p>If we were C it might make sense to have impls to bool but I don't think rust makes any judgments about "truthiness" of values in other types</p>



<a name="217481202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481202">(Nov 21 2020 at 02:52)</a>:</h4>
<p>I think several libs team members have been in favor of truncate and similar methods to replace as over time</p>



<a name="217481206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481206">(Nov 21 2020 at 02:52)</a>:</h4>
<p>So I would expect a PR to be accepted personally</p>



<a name="217481213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481213">(Nov 21 2020 at 02:53)</a>:</h4>
<p>Right, but I think the same reasoning applies to bool conversion as to u8, even though it's not a perfect match</p>



<a name="217481222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481222">(Nov 21 2020 at 02:53)</a>:</h4>
<p>I think it's important that I'm not talking about u8 though, I'm talking about Wrapping&lt;u8&gt;</p>



<a name="217481223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481223">(Nov 21 2020 at 02:53)</a>:</h4>
<p>If you're thinking of these types as maps of Z, then it makes some sense, but I don't think most people do</p>



<a name="217481265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481265">(Nov 21 2020 at 02:54)</a>:</h4>
<p>All the operations on <code>Wrapping&lt;u8&gt;</code> make sense only if you think of it as Z/256 and not 0..256</p>



<a name="217481320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481320">(Nov 21 2020 at 02:56)</a>:</h4>
<p>Well... yes? But also no, because my guess is the majority of people have not seen or really understand Z/256 as a concept. I think the idea of these maps from Wrapping to Wrapping type is not common enough to be uplifted as a From/Into impl because I would not expect to use it in a generic way</p>



<a name="217481325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481325">(Nov 21 2020 at 02:56)</a>:</h4>
<p>I think a similar argument can be had about <code>i64 -&gt; Saturating&lt;i16&gt;</code> and so on; it's not clear where else to file the method besides as a From impl</p>



<a name="217481330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481330">(Nov 21 2020 at 02:57)</a>:</h4>
<p>I think the method should be on u64</p>



<a name="217481333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481333">(Nov 21 2020 at 02:57)</a>:</h4>
<p>If we're talking about a <code>u64 -&gt; u8</code> method then it should definitely be something like <code>truncate</code> and not a from impl</p>



<a name="217481374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481374">(Nov 21 2020 at 02:58)</a>:</h4>
<p>It doesn't make a difference IMO whether it's on saturating or on wrapping or what, I think it makes sense for it to always be a method</p>



<a name="217481381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481381">(Nov 21 2020 at 02:59)</a>:</h4>
<p>(similar to how we have wrapping_mul and don't say "wrap it in Wrapping" or so)</p>



<a name="217481382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481382">(Nov 21 2020 at 02:59)</a>:</h4>
<p>But isn't that the whole point of the wrapper structs? The addition on <code>Wrapping&lt;u8&gt;</code> is not a method called <code>wrapping_add</code></p>



<a name="217481389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481389">(Nov 21 2020 at 02:59)</a>:</h4>
<p>If we had a Truncate trait, then I agree implementing it would make sense</p>



<a name="217481439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481439">(Nov 21 2020 at 03:00)</a>:</h4>
<blockquote>
<p>is there an alternative to x as u8 to represent this more explicitly</p>
</blockquote>
<p>Why is <code>as</code> so disliked? It's literally the exact thing you're looking for.</p>



<a name="217481447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481447">(Nov 21 2020 at 03:01)</a>:</h4>
<p><code>as</code> currently can be implicitly lossy when that's not necessarily what you wanted</p>



<a name="217481452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481452">(Nov 21 2020 at 03:01)</a>:</h4>
<p>But here it actually is</p>



<a name="217481461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481461">(Nov 21 2020 at 03:01)</a>:</h4>
<p>Right, but if the code doesn't say that, then you don't know when reading if it was intentional</p>



<a name="217481462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481462">(Nov 21 2020 at 03:01)</a>:</h4>
<p>and now I have to warn clippy that this is what I meant every time</p>



<a name="217481508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217481508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217481508">(Nov 21 2020 at 03:02)</a>:</h4>
<p>(there is a reason it's just a clippy lint right now)</p>



<a name="217487154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217487154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217487154">(Nov 21 2020 at 06:00)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">u64_as_u8</span><span class="p">(</span><span class="n">u</span>: <span class="kt">u64</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">u</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="217492614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217492614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217492614">(Nov 21 2020 at 09:06)</a>:</h4>
<p>I still think we need to have <code>(wrapping,checked,saturating)_cast</code> in addition to <code>(wrapping,checked,saturating)_add</code> and other arithmetic operations (<a href="https://github.com/rust-lang/rust/issues/1218">#1218</a>).</p>



<a name="217503116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217503116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217503116">(Nov 21 2020 at 14:04)</a>:</h4>
<p>how about  <code>u8::try_from(val &amp; 0xFF).unwrap()</code> ?</p>



<a name="217553695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217553695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217553695">(Nov 22 2020 at 14:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="330154">The 8472</span> <a href="#narrow/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F/near/217503116">said</a>:</p>
<blockquote>
<p>how about  <code>u8::try_from(val &amp; 0xFF).unwrap()</code> ?</p>
</blockquote>
<p>That's actually optimizing quite nicely: <a href="https://godbolt.org/z/Yf8TGf">https://godbolt.org/z/Yf8TGf</a></p>



<a name="217570038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217570038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217570038">(Nov 22 2020 at 21:07)</a>:</h4>
<p>i think <code>val as u8</code> is still the correct thing here, and clippy just needs to hush some of the time.</p>



<a name="217574690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217574690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217574690">(Nov 22 2020 at 22:55)</a>:</h4>
<p>I am currently spelling this operation <code>#[allow(clippy::cast_possible_truncation)] { val as u8 }</code>, which is more verbose than some of the options but at least gets the point across</p>



<a name="217577672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Alternative%20to%20%60as%60%20for%20truncation%3F/near/217577672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F.html#217577672">(Nov 23 2020 at 00:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123856">Vadim Petrochenkov</span> <a href="#narrow/stream/122651-general/topic/Alternative.20to.20.60as.60.20for.20truncation.3F/near/217492614">said</a>:</p>
<blockquote>
<p>I still think we need to have <code>(wrapping,checked,saturating)_cast</code> in addition to <code>(wrapping,checked,saturating)_add</code> and other arithmetic operations (<a href="https://github.com/rust-lang/rust/issues/1218">#1218</a>).</p>
</blockquote>
<p>See also the proposal for <code>wrapping_from</code> and friends in <a href="https://github.com/rust-lang/rfcs/pull/2484">https://github.com/rust-lang/rfcs/pull/2484</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>